// n.d(e, { O: () => w, rM: () => p, U$: () => d });
import * as THREE from "three"

const i = new THREE.Vector3(),
  s = new THREE.Vector3(),
  o = new THREE.Vector3(),
  a = new THREE.Vector2(),
  c = new THREE.Vector2(),
  u = new THREE.Vector2(),
  l = new THREE.Vector3()
function f(t, e, n, f, h, d, p): any {
  i.fromBufferAttribute(e, f), s.fromBufferAttribute(e, h), o.fromBufferAttribute(e, d)
  const w: any = (function (t, e, n, i, s, o) {
    let a
    return (
      (a = o === THREE.BackSide ? t.intersectTriangle(i, n, e, !0, s) : t.intersectTriangle(e, n, i, o !== THREE.DoubleSide, s)),
      null === a ? null : { distance: t.origin.distanceTo(s), point: s.clone() }
    )
  })(t, i, s, o, l, p)
  if (w) {
    n &&
      (a.fromBufferAttribute(n, f),
      c.fromBufferAttribute(n, h),
      u.fromBufferAttribute(n, d),
      (w.uv = THREE.Triangle.getUV(l, i, s, o, a, c, u, new THREE.Vector2())))
    const t = {
      a: f,
      b: h,
      c: d,
      normal: new THREE.Vector3(),
      materialIndex: 0
    }
    THREE.Triangle.getNormal(i, s, o, t.normal), (w.face = t), (w.faceIndex = f)
  }
  return w
}
function h(t, e, n, r, i?) {
  const s = 3 * r,
    o = t.index.getX(s),
    a = t.index.getX(s + 1),
    c = t.index.getX(s + 2),
    u = f(n, t.attributes.position, t.attributes.uv, o, a, c, e)
  return u ? ((u.faceIndex = r), i && i.push(u), u) : null
}
function d(t, e, n, r, i, s) {
  for (let o = r, a = r + i; o < a; o++) h(t, e, n, o, s)
}
function p(t, e, n, r, i) {
  let s = 1 / 0,
    o = null
  for (let a = r, c = r + i; a < c; a++) {
    const r = h(t, e, n, a)
    r && r.distance < s && ((o = r), (s = r.distance))
  }
  return o
}
function w(t, e: THREE.Mesh, n: THREE.Raycaster) {
  if (t) {
    t.point.applyMatrix4(e.matrixWorld)
    t.distance = t.point.distanceTo(n.ray.origin)
    t.object = e
    return t.distance < n.near || t.distance > n.far ? null : t
  }
  return null
}
export default {
  O: w,
  rM: p,
  U$: d
}
